﻿@model Domain.Question

@{
    ViewBag.Title = Model.Title;
}

@section facebookMetaTags
{
    <meta property="og:title" content="Cantinho da cegonha - Comunidade"/>
    <meta property="og:type" content="article"/>
    <meta property="og:url" content="@Request.Url"/>
    <meta property="og:description" content="Cantinho da cegonha - Comunidade"/>
    <meta property="og:image" content="http://www.cantinhodacegonha.com/content/images/cegonha_2.png"/>
    <meta property="og:site_name" content="Cantinho da cegonha"/>
    <meta property="fb:page_id" content="231110590236634"/>
}

<script type="text/javascript">
    $(document).ready(function () {
        // Executed once all the page elements are loaded

        lastVal = totHistory;
        $('.vote_tip').tipsy({ gravity: 'w' });
        $('.correcta').tipsy({ gravity: 'w' });
    });

    var totHistory = 0;
    // Holds the number of comments

    var positions = new Array();
    var lastVal;

    function addHistory(obj) {
        /* Gets called on page load for each comment, and on comment submit */
        totHistory++;
        positions.push(obj.id);
    }

    function buildQ(from, to) {
        /* Building a jQuery selector from the begin
        and end point of the slide */

        if (from > to) {
            var tmp = to;
            to = from;
            from = tmp;
        }

        from++;
        to++;

        var query = '';
        for (var i = from; i < to; i++) {
            if (i != from) query += ',';
            query += '.com-' + positions[i - 1];
        }

        /* Each comment has an unique com-(Comment ID) class
        that we are using to address it */

        return query;
    }


    function addComment(where, parent,imgUrl,questionId) {
        /*	This functions gets called from both the "Add a comment" button
        on the bottom of the page, and the add a reply link.
        It shows the comment submition form */

        var $el;
        if ($('.waveButton').length) return false;
        // If there already is a comment submition form
        // shown on the page, return and exit

        if (!where)
            $el = $('#commentArea');
        else
            $el = $(where).closest('.waveComment');

        if (!parent) parent = 0;

        // If we are adding a comment, but there are hidden comments by the slider:
        $('.waveComment').show('slow');
        lastVal = totHistory;

        $('#slider').slider('option', 'value', totHistory);
        // Move the slider to the end point and show all comments
        var comment = '<div class="waveComment addComment">\
	\
	<div class="comment">\
	<div class="commentAvatar">\
	<img src="'+imgUrl+'" width="30" height="30" />\
	</div>\
	\
	<div class="commentText">\
	\
	<textarea class="textArea" rows="2" cols="70" name="" />\
	<div><input type="button" class="waveButton" value="Comentar" onclick="addSubmit(this,' + parent + ','+questionId+')" /> ou <a href="" onclick="cancelAdd(this);return false">cancelar</a></div>\
	\
	</div>\
	</div>\
	\
	</div>';

        $el.append(comment);
        // Append the form
    }

    function cancelAdd(el) {
        $(el).closest('.waveComment').remove();
    }

    function addSubmit(el, parent,questionId) {
        /* Executed when clicking the submit button */
        var cText = $(el).closest('.commentText');
        var text = cText.find('textarea').val();
        var wC = $(el).closest('.waveComment');
        if (text.length < 4) {
            alert("Your comment is too short!");
            return false;
        }

        $.ajax({
            type: "POST",
            url: "../../Resposta/SaveComment",
            data: "comment=" + encodeURIComponent(text) + "&parent=" + parent + "&questionId=" + questionId,
            /* Sending both the text and the parent of the comment */
            success: function (msg) {

                /* PHP returns the automatically assigned ID of the new comment */
                var ins_id = parseInt(msg.id);
                if (ins_id) {
                    wC.addClass('com-' + ins_id);
                    addHistory({ id: ins_id });
                    lastVal = totHistory;
                }
                nome = msg.nome;
                transForm(text, cText, nome);
                // Hiding the form and showing the newly-added comment in its place
            }
        });
    }

    function transForm(text, cText,nome) {
        var tmpStr = '<span class="name">'+nome+':</span> ' + text;
        cText.html(tmpStr);
    }

    function vote(id, action) {
        $.ajax({
            type: "POST",
            url: "../../Resposta/Vote",
            data: "id=" + id + "&action=" + action,
            success: function (msg) {
                if (msg.success == "false" || msg.success == "votoproprio" || msg.success == "javotou") {
                    return;
                }
                $("#votos-" + msg.id + " .vote-count-post").html(msg.votos);
            }
        });
    }

    function votePerg(id, action) {
        $.ajax({
            type: "POST",
            url: "../Vote",
            data: "id=" + id + "&action=" + action,
            success: function (msg) {
                if (msg.success == "false" || msg.success == "votoproprio" || msg.success == "javotou") {
                    return;
                }
                $("#votosP-" + msg.id + " .vote-count-post").html(msg.votos);
            }
        });
    }

    function marcaCorrecta(idresp,idperg) {
        $.ajax({
            type: "POST",
            url: "../../Resposta/MarcarCorrecta",
            data: "idresp=" + idresp + "&idperg=" + idperg,
            success: function (msg) {
                if (msg.success == "false") {
                    $("#corr-" + idresp).toggleClass("correcta");
                    return;
                }
                $("#corr-" + idresp).toggleClass("correctaMarcada");

            }
        });
    }

    function votedisabled() {
        $(".signin").addClass('menu-open');
        $("#signin_menu").css("display", "block");
        $('html, body').animate({ scrollTop: 0 }, 'slow');
    }

</script>


<div class="leftCol">
@Html.Partial("_LeftMenu")
<div class="separator_green">
@Html.Partial("_PubVertical")
</div>
<div class="clear"></div>
</div>
<div class="centerCol" style="width:735px;">
@Html.Partial("_Social")
<div style="float:left">@Html.ActionLink("Voltar", "Index","Pergunta")</div>
<h1 style="text-align:center">@Model.Title</h1>
<br />
<table>
    <tbody><tr>
    <td class="votecell">
        
        <div id="votosP-@Model.Id" style="float: left; width: 20px;text-align: center;">
            @if (User.Identity.IsAuthenticated)
            {
                <a class="vote-up-off vote_tip" onclick="votePerg(@Model.Id,1);" title="Esta pergunta demonstra pesquisa e esforço. É útil e clara.">up vote</a>
                <span class="vote-count-post">@Model.Votes</span>
                <a class="vote-down-off vote_tip" onclick="votePerg(@Model.Id,0);" title="Esta pergunta demonstra pouca pesquisa e esforço; Não é útil nem clara.">down vote</a>  
            }
            else
            {
                <a class="vote-up-off vote_tip" onclick="votedisabled();" title="Se esta pergunta foi útil, faça LOGIN e vote!">up vote</a>
                <span class="vote-count-post">@Model.Votes</span>
                <a class="vote-down-off vote_tip" onclick="votedisabled();" title="Se esta pergunta não foi útil, faça LOGIN e vote!">down vote</a>  
            }
        </div>

    </td>

<td class="postcell">
<div>          
    <div class="post-text">
        <p>@Model.Body</p>
    </div>            
    <div class="post-taglist">
            @foreach (var tag in Model.Tag)
            {
                @Html.ActionLink(tag.TagName,"Tagged","Pergunta",new {id = tag.TagName},new {@class ="post-tag",title ="Mostrar perguntas com a palavra chave '"+tag.TagName+"'",rel="tag"})
               
            }        
    </div>
    <table class="fw">
    <tbody><tr>
    <td class="vt">
    <div class="post-menu">
     @if (User.Identity.Name == Model.User.Email)
     {
        @Html.ActionLink("editar", "Edit", new { id = Model.Id })
     }</div>
    </td>
                        

    <td class="post-signature owner">
    <div class="user-info"><div class="user-action-time">perguntado <span title="@Model.DateCreated" class="relativetime">@CantinhodaCegonha.Models.Helpers.Util.GetDataPassada(Model.DateCreated)</span></div><div class="user-gravatar32"><a href="@Url.Action("UserDetails", "Account", new { id =Model.UserId })"><img src="@Model.User.Foto" height="32" width="32" alt=""></a></div><div class="user-details">@Html.ActionLink(Model.User.Nome, "UserDetails", "Account",new { id =Model.UserId },null)<br><span class="reputation-score" title="reputation score">@if(Model.User.Ponto == null){<text>0</text> }else{@Model.User.Ponto.Total}</span></div></div>
    </td>
    </tr>
    </tbody></table>
</div>
</td>
    </tr>


<tr>
<td class="votecell"></td>
<td>
    <div id="comments-6843114" class="comments dno">
        <table>
        <tbody>
        
            <tr><td></td><td></td></tr>
        
        </tbody>
    
        </table>
    </div>
    
</td>
</tr>    </tbody></table>

<div id="answers-header">
        <div class="subheader answers-subheader">
            <h2 class="h2_green">@Model.Answer.Count Respostas</h2>
        </div>
        </div>
<div class="separator_green" style="margin-bottom: 0px;"></div>
<div class="clear"></div>
@if (User.Identity.IsAuthenticated)
{
    if (Model.Answer.Count > 2)
    {
	<div style="margin-bottom:15px"><input type="button"  value="Contribua com a sua resposta" onclick="addComment(null,null,'@CantinhodaCegonha.Models.Helpers.Helpers.GetLogedUserImage(User.Identity.Name)',@Model.Id);$('html, body').animate({scrollTop: $(document).height()}, 800);" /></div>
    }
}
else
{
    if (Model.Answer.Count > 2)
    {
        <div style="margin-bottom:15px"> @Html.ActionLink("Faça login, contribua com a sua resposta", "LogOn", "Account", new { returnurl = Request.Url.AbsolutePath }, null)</div>
    }
}
<div id="commentArea">
@foreach (var answer in Model.Answer.OrderByDescending(x => x.Votes).ThenByDescending(x => x.DateCreated))
{
   <div class="waveComment com-@answer.Id">
       <div class="comment">
            <div class="waveTime">@answer.DateCreated</div>
            <div class="commentAvatar">
                <img width="30" height="30" alt="" src="@answer.User.Foto"/>
            </div>
            <div class="commentText">
                <span class="name">@answer.User.Nome:</span><div style="margin-left:10px;">@answer.Text</div>
		    </div>
            <div class="replyLink">
            @if (User.Identity.IsAuthenticated)
            {
				<a onclick="addComment(this,@answer.Id,'@CantinhodaCegonha.Models.Helpers.Helpers.GetLogedUserImage(User.Identity.Name)',@Model.Id);return false;" href="">Comente esta resposta »</a>
            }
            else
            {
                @Html.ActionLink("Faça login para comentar", "LogOn", "Account", new { returnurl = Request.Url.AbsolutePath }, null)
            }
			</div>
			<div class="clear"></div>   
        </div>
        <div id="votos-@answer.Id" style="float: left; width: 20px;text-align: center;">
             @if (User.Identity.IsAuthenticated)
             {
                <a class="vote-up-off vote_tip" onclick="vote(@answer.Id,1);" title="Esta resposta demonstra pesquisa e esforço. É útil e clara.">up vote</a>
                <span class="vote-count-post">@answer.Votes</span>
                <a class="vote-down-off vote_tip" onclick="vote(@answer.Id,0);" title="Esta resposta demonstra pouca pesquisa e esforço; Não é útil nem clara.">down vote</a>  
             }
             else
             {
                <a class="vote-up-off vote_tip" onclick="votedisabled();" title="Se esta pergunta foi útil, faça LOGIN e vote!">up vote</a>
                <span class="vote-count-post">@answer.Votes</span>
                <a class="vote-down-off vote_tip" onclick="votedisabled();" title="Se esta pergunta não foi útil, faça LOGIN e vote!">down vote</a>  
             }
        </div>
        @if (User.Identity.Name == Model.User.Email)
        {
            <div style="float: right; margin-top: 16px; margin-right: -7px;">
                <a id="corr-@answer.Id" class="correcta" style="cursor: pointer;" onclick="marcaCorrecta(@answer.Id,@answer.QuestionId);" title="Marcar esta resposta como a mais correcta!"></a>
            </div>
        }
        <div class="clear"></div>
        @foreach (var comment in answer.Comment)
        {
            <div class="waveComment com-@comment.Id">
            <div class="comment">
            <div class="waveTime">@comment.Data</div>
            <div class="commentAvatar">
                <img width="30" height="30" alt="" src="@comment.User.Foto"/>
            </div>
            <div class="commentText">
                <span class="name">@comment.User.Nome:</span><div style="margin-left:10px;">@comment.Body</div>
		    </div>
            <div class="replyLink">
			</div>
			<div class="clear"></div>   
        </div>

   </div>
        }
   </div>
}
</div>
<div class="clear"></div>
@if (User.Identity.IsAuthenticated)
{
    string texto = "Contribua com a sua resposta";
    if (Model.Answer.Count == 0)
    { texto = "Seja o primeiro a contribuir com a uma resposta"; }
	<div style="margin-top:15px"><input type="button"  value="@texto" onclick="addComment(null,null,'@CantinhodaCegonha.Models.Helpers.Helpers.GetLogedUserImage(User.Identity.Name)',@Model.Id)" /></div>
}
else
{
    string texto = "Faça login, contribua com a sua resposta";
    if (Model.Answer.Count == 0)
    { texto = "Faça login, seja o primeiro a contribuir com a uma resposta"; }
   <div style="margin-top:15px"> @Html.ActionLink(texto, "LogOn", "Account", new { returnurl = Request.Url.AbsolutePath }, null)</div>
}

</div>
<div class="clear"></div>